!run_service.

+!run_service : true <- 
	createWorkspace("web-services");
	joinWorkspace("web-services", WspID);
	.print("web-services workspace created and join done");
	!setup_tools;
	cartago.new_obj("cartagows.WSMsgBasicFilter",["GetTime|GetDay"],Filter);
	.print("Service ready, waiting messages");
	!wait_msg(Filter).
  	 
	
+!wait_msg(Filter) : true <-
	getWSMsgWithFilter(Filter, Msg);
	cartago.invoke_obj(Msg,getOperationName,OpName);
	!process_request(OpName,Msg);
	!!wait_msg(Filter).
    
+!process_request("GetTime", Msg): true <-
	.print("processing GetTime.");
	!get_time(Msg,Reply);
	sendWSReply(Msg,Reply, ReplyID);
	.print("GetTime request processed, reply id ", ReplyID).

+!get_time(MsgIn,Reply) : xmlManager(XMLManager) <-
	cartago.invoke_obj(MsgIn,getBody,MsgBody);
	cartago.invoke_obj(XMLManager,buildElementFromString(MsgBody),Elem);
	cartago.invoke_obj(XMLManager,getElementValue("GetTimeRequest","time",Elem),TickerSymbol);
	getTime(Time);
	.concat("<tns:TradeTime xmlns:tns='http://example.com/timeprovider.wsdl'><tns:time>",Time,"</tns:time></tns:TradeTime>",Reply).
	
+!process_request("GetDay", Msg): true <-
	.print("processing GetDay.");
	!get_day(Msg,Reply);
	sendWSReply(Msg,Reply, ReplyID);
	.print("GetDay request processed, reply id ", ReplyID).

+!get_day(MsgIn,Reply) : xmlManager(XMLManager) <-
	cartago.invoke_obj(MsgIn,getBody,MsgBody);
	cartago.invoke_obj(XMLManager,buildElementFromString(MsgBody),Elem);
	cartago.invoke_obj(XMLManager,getElementValue("GetDayRequest","day",Elem),TickerSymbol);
	getDay(Day);
	.concat("<tns:TradeDay xmlns:tns='http://example.com/timeprovider.wsdl'><tns:day>",Day,"</tns:day></tns:TradeDay>",Reply).

-!setup_tools[error(E),error_msg(Msg),code(Cmd),code_src(Src),code_line(Line)]   <- .print("Failed to start due to: ",Msg," (",E,"). Command: ",Cmd, " on ",Src,":", Line).
+!setup_tools: true <-
    .print("Creating CWSServerArtifact");
    makeArtifact("CWSServerArtifact", "cartagows.CWSServerArtifact",[],ManagerID);
    makeArtifact("TimeArtifact", "cartagows.TimeProviderArtifact",[],TimeID);
    .print("CWSServerArtifact created");
    startServer[artifact_id(ManagerID)];
    .print("CWSServerArtifact created and CArtAgO-WS service started.");

	cartago.new_obj("cartagows.util.XMLibJason",[],XMLManager);
	+xmlManager(XMLManager);
  
	!make_panel;
	.print("tools built.").
 
+!make_panel : true	<-	 
	makeArtifact("TimeproviderService","cartagows.WSPanel", ["data/timeprovider.wsdl","TimeproviderService"], WSPanelID);
	.print("ws-panel built.");
	.print("ws-panel focussed.").